package com.cat.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cat.entity.*;
import com.cat.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController{
    @Reference
    private HouseService houseService;
    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;
    @Reference
    private HouseImageService houseImageService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private HouseUserService houseUserService;
    private final static String LIST_ACTION = "redirect:/house";
    private final static String PAGE_INDEX = "house/index";
    private final static String PAGE_SHOW = "house/show";
    private final static String PAGE_CREATE = "house/create";
    private final static String PAGE_EDIT = "house/edit";
    private final static String PAGE_SUCCESS = "common/successPage";

    @RequestMapping
    public String index(Map map, HttpServletRequest httpServletRequest){
        Map<String,Object> filters = getFilters(httpServletRequest);
        PageInfo<House> page = houseService.findPage(filters);
        map.put("page", page);
        map.put("filters", filters);
        map.put("communityList",communityService.findAll());
        map.put("houseTypeList",dictService.findDictByDictCode("houseType"));
        map.put("floorList",dictService.findDictByDictCode("floor"));
        map.put("buildStructureList",dictService.findDictByDictCode("buildStructure"));
        map.put("directionList",dictService.findDictByDictCode("direction"));
        map.put("decorationList",dictService.findDictByDictCode("decoration"));
        map.put("houseUseList",dictService.findDictByDictCode("houseUse"));
        return PAGE_INDEX;
    }
    @RequestMapping("/create")
    public String create(Map map){
        map.put("communityList",communityService.findAll());
        map.put("houseTypeList",dictService.findDictByDictCode("houseType"));
        map.put("floorList",dictService.findDictByDictCode("floor"));
        map.put("buildStructureList",dictService.findDictByDictCode("buildStructure"));
        map.put("directionList",dictService.findDictByDictCode("direction"));
        map.put("decorationList",dictService.findDictByDictCode("decoration"));
        map.put("houseUseList",dictService.findDictByDictCode("houseUse"));
//        map.put("house",house);
        return PAGE_CREATE;
    }
    @RequestMapping("/edit/{id}")
    public String edit(Map map, @PathVariable Long id){
        House house = houseService.getById(id);
        map.put("house",house);
        map.put("communityList",communityService.findAll());
        map.put("houseTypeList",dictService.findDictByDictCode("houseType"));
        map.put("floorList",dictService.findDictByDictCode("floor"));
        map.put("buildStructureList",dictService.findDictByDictCode("buildStructure"));
        map.put("directionList",dictService.findDictByDictCode("direction"));
        map.put("decorationList",dictService.findDictByDictCode("decoration"));
        map.put("houseUseList",dictService.findDictByDictCode("houseUse"));
        return PAGE_EDIT;
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        houseService.delete(id);
        return LIST_ACTION;
    }
    @RequestMapping("/save")
    public String save(House house){
        houseService.insert(house);
        return LIST_ACTION;
    }
    /**
     * 发布
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/publish/{id}/{status}")
    public String publish(@PathVariable Long id,@PathVariable Integer status) {
        houseService.publish(id, status);
        return LIST_ACTION;
    }
    @GetMapping("/{id}")
    public String show(Map map,@PathVariable Long id){
        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        List<HouseUser> houseUserList = houseUserService.findListByHouseId(id);
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        List<HouseImage> houseImage2List = houseImageService.findList(id, 2);

        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseUserList",houseUserList);
        map.put("houseImage1List",houseImage1List);
        map.put("houseImage2List",houseImage2List);
        return PAGE_SHOW;
    }
}
